<?php

class Core {

	// Function to validate the post data
	function validate_post($data)
	{
		// Counter variable
		$counter = 0;

		// Validate the hostname
		if(isset($data['hostname']) AND !empty($data['hostname'])) {
			$counter++;
		}
		// Validate the username
		if(isset($data['username']) AND !empty($data['username'])) {
			$counter++;
		}
		// Validate the password
		if(isset($data['password']) AND !empty($data['password'])) {
		  // pass
		}
		// Validate the database
		if(isset($data['database']) AND !empty($data['database'])) {
			$counter++;
		}

		// Check if all the required fields have been entered
		if($counter == '3') {
			return true;
		}
		else {
			return false;
		}
	}

	// Function to show an error
	function show_message($type,$message) {
		return $message;
	}

	// Function to write the config file
	function write_config($data) {

		// DB Config path
		$template_path 	= 'config/database.php';
		$output_path 	= '../application/config/database.php';

		// Open the file
		$database_file = file_get_contents($template_path);

		$new  = str_replace("%HOSTNAME%",$data['hostname'],$database_file);
		$new  = str_replace("%USERNAME%",$data['username'],$new);
		$new  = str_replace("%PASSWORD%",$data['password'],$new);
		$new  = str_replace("%DATABASE%",$data['database'],$new);
		$new  = str_replace("%DBPREFIX%",$data['database_prefix'],$new);


		// Write the new database.php file
		$handle = fopen($output_path,'w+');

		// Chmod the file, in case the user forgot
		@chmod($output_path,0777);

		// Verify file permissions
		if(is_writable($output_path)) {

			// Write the file
			if(fwrite($handle,$new)) {
				$db_success = true;
			} else {
				$db_success = false;
			}

		} else {
			$db_success = false;
		}

		// CI Config path
		$template_path 	= 'config/config.php';
		$output_path 	= '../application/config/config.php';

		// Open the file
		$config_file = file_get_contents($template_path);

    $characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()-+_={}[]<>,.?";
    $randomString = '';
    for ($i = 0; $i < 32; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }


		$new  = str_replace("%BASEURL%",$data['baseurl'],$config_file);
		$new  = str_replace("%ENCRYPTIONKEY%",$randomString,$new);
		$new  = str_replace("%DBPREFIX%",$data['database_prefix'],$new);


		// Write the new config.php file
		$handle = fopen($output_path,'w+');

		// Chmod the file, in case the user forgot
		@chmod($output_path,0777);

		// Verify file permissions
		if(is_writable($output_path)) {

			// Write the file
			if(fwrite($handle,$new)) {
				$config_success = true;
			} else {
				$config_success = false;
			}

		} else {
			$config_success = false;
		}

		// Auth Config path
		$template_path 	= 'config/ion_auth.php';
		$output_path 	= '../application/config/ion_auth.php';

		// Open the file
		$auth_file = file_get_contents($template_path);

		$new  = str_replace("%SITETITLE%",$data['sitename'],$auth_file);
		$new  = str_replace("%ADMINEMAIL%",$data['adminemail'],$new);
		$new  = str_replace("%DBPREFIX%",$data['database_prefix'],$new);


		// Write the new config.php file
		$handle = fopen($output_path,'w+');

		// Chmod the file, in case the user forgot
		@chmod($output_path,0777);

		// Verify file permissions
		if(is_writable($output_path)) {

			// Write the file
			if(fwrite($handle,$new)) {
				$auth_success = true;
			} else {
				$auth_success = false;
			}

		} else {
			$auth_success = false;
		}

		$password   = $this->hash_password($data['adminpassword'], false);
		$username   = 'Admin';
		$email			= $data['adminemail'];

		$mysqli = new mysqli($data['hostname'],$data['username'],$data['password'],$data['database']);
		
		$query = "INSERT INTO `".$data['database_prefix']."users` (`username`,`email`,`password`,`created_on`, `active`) 
																									 VALUES ('".$username."', '".$email."', '".$password."',' ".time()."', 1)";

		$result = $mysqli->query($query);
		
		$query = "SELECT * FROM `".$data['database_prefix']."users` WHERE `username` = '".$username."'";
		$new_user = $mysqli->query($query);
		$row = $new_user->fetch_assoc();
//insert into admin group here
		$query = "INSERT INTO `".$data['database_prefix']."users_groups` (`id`, `user_id`, `group_id`) VALUES (NULL,'".$row['id']."','1')";
		//echo $query;
		$group_result = $mysqli->query($query);
		return $config_success && $db_success && $auth_success && $result && $group_result;
	}

	public function hash_password($password)
	{
		if (empty($password))
		{
			return FALSE;
		}
			$salt = $this->salt();
			return  $salt . substr(sha1($salt . $password), 0, -10);

	}

	public function salt()
	{
		return substr(md5(uniqid(rand(), true)), 0, 10);
	}

}